package pgpchat;

import java.awt.EventQueue;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class MainFrame extends JFrame {

	private final JPanel contentPane;
	private final JTextField txtLogin;
	private final JPasswordField txtSenha;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setTitle("..::PGP chat::..");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 449, 280);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		txtLogin = new JTextField();
		txtLogin.setBounds(43, 54, 294, 20);
		contentPane.add(txtLogin);
		txtLogin.setColumns(10);

		JButton btnNewButton = new JButton("Conectar a servidor");
		btnNewButton.addMouseListener(conectaServidor());
		btnNewButton.setBounds(179, 177, 158, 23);
		contentPane.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("Criar servidor");
		btnNewButton_1.addMouseListener(criaServidor());
		btnNewButton_1.setBounds(43, 177, 112, 23);
		contentPane.add(btnNewButton_1);

		JLabel lblNewLabel = new JLabel("Login");
		lblNewLabel.setBounds(41, 37, 46, 14);
		contentPane.add(lblNewLabel);

		JLabel lblNewLabel_1 = new JLabel("Senha");
		lblNewLabel_1.setBounds(43, 115, 46, 14);
		contentPane.add(lblNewLabel_1);

		txtSenha = new JPasswordField();
		txtSenha.setBounds(43, 132, 294, 20);
		contentPane.add(txtSenha);
	}

	/**
	 * @return
	 */
	private MouseAdapter conectaServidor() {
		return new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (MainFrame.this.valida()) {
					String ip = JOptionPane.showInputDialog("digite o ip");
					int porta = Integer.parseInt(JOptionPane.showInputDialog("digite a porta"));
					if (porta < 65535) {
						new ChatPanel(MainFrame.this.txtLogin.getText(), MainFrame.this.txtSenha.getText(), ip, porta);
						MainFrame.this.dispose();
					} else {
						JOptionPane.showMessageDialog(null, "Porta inv�lida");
					}
				}
			}
		};
	}

	/**
	 * @return
	 */
	private MouseAdapter criaServidor() {
		return new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (MainFrame.this.valida()) {
					int porta = Integer.parseInt(JOptionPane.showInputDialog("digite a porta"));
					if (porta < 65535) {
						new ChatPanel(MainFrame.this.txtLogin.getText(), MainFrame.this.txtSenha.getText(), porta);
						MainFrame.this.dispose();
					} else {
						JOptionPane.showMessageDialog(null, "Porta inválida");
					}
				}
			}
		};
	}

	private boolean valida() {
		if (this.txtLogin.getText().isEmpty() || this.txtSenha.getText().isEmpty()) {
			JOptionPane.showMessageDialog(this, "Preencha os campos...");
			return false;
		}
		return true;
	}
}
